/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package supermarketv1;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import javax.swing.JOptionPane;
import supermarketv0_class.*;


/**
 *
 * @author ΠΑΝΑΓΙΩΤΗΣ v1
 */
public class AddCustomer extends javax.swing.JFrame {

    SuperMarketMainView1 mainscreen;
    Customer customer;
    String buttonText;
    String id, fname, lname, address, codepass;
    int ID, cardnum;
    ArrayList bank;
    Bank b;
    ArrayList cca;
    CreditCardAuthority c;
    
    /**
     * Creates new form AddCustomer
     */
    public AddCustomer(SuperMarketMainView1 mainscreen, Customer customer, String buttonText) {
        this.mainscreen = mainscreen;
        this.customer = customer;
        bank = new ArrayList();

        initComponents();
        jTextField1.disable();
        jComboBox1.removeAllItems();
        bank = mainscreen.apk.readBanks();
        jComboBox1.removeAllItems();//ΚΑΘΑΡΙΣΜΟΣ
      //  jComboBox1.addItem("Select Bank");
        Iterator itr = bank.iterator();
        while (itr.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗ ΛΙΣΤΑ
            Bank ba = (Bank) itr.next();
            jComboBox1.addItem(ba.getS());//ΠΡΟΣΘΗΚΗ ΣΤΟ  combobox
        }

        if (buttonText.equals(mainscreen.custmanag.b1)) {// αν Δημιουργία
            jLabel6.setText("Δημιουργία Νέου Πελάτη");
            jTextField1.setVisible(false);
            
            
            
            
        } else if (buttonText.equals(mainscreen.custmanag.b2)) {//αν Τροποποίηση
            jLabel6.setText("Τροποποίηση Πελάτη");
            jTextField1.setText(Integer.toString(customer.getCustomerId()));
            jTextField2.setText(customer.getFirstName());
            jTextField3.setText(customer.getLastName());
            jTextField4.setText(customer.getAddress());
            jTextField5.setText(customer.getCodepass());
                //jComboBox1.removeAllItems();
             bank = mainscreen.apk.readBanks();
             jComboBox1.removeAllItems();//ΚΑΘΑΡΙΣΜΟΣ
      
             Iterator itrs = bank.iterator();
       while (itrs.hasNext()) {//ΔΙΑΤΡΕΧΩ ΤΗ ΛΙΣΤΑ
            Bank ba = (Bank) itrs.next();
            
            jComboBox1.addItem(ba.getS());//ΠΡΟΣΘΗΚΗ ΣΤΟ  combobox
         //   jComboBox1.setSelectedIndex(b.hashCode());
            
       }
               // String car = (jTextField8.getText());//μετατροπή
              // int cvv = Integer.parseInt(jTextField9.getText());//μετατροπή
                //καταχώρηση στοιχείων σε εξωτερική Βαση Δεδομενων
             //  System.out.println(b.getBankId() + " " + car + " " + jTextField7.getText() + " " + cvv);

              //  mainscreen.apk.getRecord(Integer.parseInt(jTextField9.getText()));
       
            jTextField1.setVisible(true);
        
        } else {
            jLabel6.setText("Διαγραφή Πελάτη");
            jTextField1.setText(Integer.toString(customer.getCustomerId()));
            jTextField2.setText(customer.getFirstName());
            jTextField3.setText(customer.getLastName());
            jTextField4.setText(customer.getAddress());
            jTextField5.setText(customer.getCodepass());

            jTextField1.setVisible(true);
            jTextField2.disable();
            jTextField3.disable();
            jTextField4.disable();
            jTextField5.disable();

            jTextField7.disable();
            jTextField8.disable();
            jTextField9.disable();

        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jTextField5 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextField7 = new javax.swing.JTextField();
        jTextField8 = new javax.swing.JTextField();
        jTextField9 = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(supermarketv1.SuperMarketv1App.class).getContext().getResourceMap(AddCustomer.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jTextField4.setText(resourceMap.getString("jTextField4.text")); // NOI18N
        jTextField4.setName("jTextField4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jTextField5.setText(resourceMap.getString("jTextField5.text")); // NOI18N
        jTextField5.setName("jTextField5"); // NOI18N

        jPanel1.setBackground(resourceMap.getColor("jPanel1.background")); // NOI18N
        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        jPanel1.setName("jPanel1"); // NOI18N

        jLabel7.setText(resourceMap.getString("jLabel7.text")); // NOI18N
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText(resourceMap.getString("jLabel8.text")); // NOI18N
        jLabel8.setName("jLabel8"); // NOI18N

        jLabel9.setText(resourceMap.getString("jLabel9.text")); // NOI18N
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText(resourceMap.getString("jLabel10.text")); // NOI18N
        jLabel10.setName("jLabel10"); // NOI18N

        jTextField7.setText(resourceMap.getString("jTextField7.text")); // NOI18N
        jTextField7.setName("jTextField7"); // NOI18N

        jTextField8.setText(resourceMap.getString("jTextField8.text")); // NOI18N
        jTextField8.setName("jTextField8"); // NOI18N
        jTextField8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField8ActionPerformed(evt);
            }
        });

        jTextField9.setText(resourceMap.getString("jTextField9.text")); // NOI18N
        jTextField9.setName("jTextField9"); // NOI18N

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel9)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField9, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE)
                    .addComponent(jTextField7)
                    .addComponent(jTextField8, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jComboBox1, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel7)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jTextField7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(jTextField8, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel10))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField9, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(66, Short.MAX_VALUE))
        );

        jLabel6.setFont(resourceMap.getFont("jLabel6.font")); // NOI18N
        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(73, 73, 73)
                                .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField4)
                                    .addComponent(jTextField5)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel1))
                                .addGap(62, 62, 62)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24))))
            .addGroup(layout.createSequentialGroup()
                .addGap(236, 236, 236)
                .addComponent(jButton1)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(15, 15, 15)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 47, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here: κουμπί ΟΚ
        buttonText = jLabel6.getText();
        if (buttonText.equals("Δημιουργία Νέου Πελάτη")) {

            fname = jTextField2.getText();
            lname = jTextField3.getText();
            address = jTextField4.getText();
            codepass = jTextField5.getText();
          int cv = Integer.parseInt(jTextField9.getText());
     
       int cca =  (Integer) mainscreen.apk.getRecord(cv);//ανάγνωση κάρτας        
             
           
            if (jTextField9.getText().isEmpty()) {

            } else  if (cca>0)  {
                 JOptionPane.showMessageDialog(null, "Ο αριθμός CVV υπάρχει: " + cca + "", "Πληκτρολόγησε ένα διαφορετικό CVV", JOptionPane.WARNING_MESSAGE);
           
            } else {
                
           Iterator itr = bank.iterator();
      
            Bank b = (Bank) itr.next();
                   
               b.equals(jComboBox1.getSelectedIndex()); //πάρε την τράπεζα
                
            
                String car = (jTextField8.getText());//μετατροπή
                int cvv = Integer.parseInt(jTextField9.getText());//μετατροπή
                //καταχώρηση στοιχείων σε εξωτερική Βαση Δεδομενων
               System.out.println(b.getBankId() + " " + car + " " + jTextField7.getText() + " " + cvv);

                mainscreen.apk.newRecord(b.getBankId(), jTextField8.getText(), jTextField7.getText(), Integer.parseInt(jTextField9.getText()));
         
           
                
                
            }
            // Τυχαία παραγωγή αριθμού κάρτας
            Random rad = new Random();
            cardnum = rad.nextInt(1000000) + 111111;

            mainscreen.db.NewCustomer(fname, lname, address, codepass, cardnum, mainscreen.db.readCustomers());
            JOptionPane.showMessageDialog(null, "Ο αριθμός: " + cardnum + "", "Δημιουργία Κάρτας", JOptionPane.WARNING_MESSAGE);
            jComboBox1.removeAllItems();
            dispose();

        } else if (buttonText.equals("Τροποποίηση Πελάτη")) {

            fname = jTextField2.getText();
            lname = jTextField3.getText();
            address = jTextField4.getText();
            codepass = jTextField5.getText();
            mainscreen.db.ModifyCustomer(customer, fname, lname, address, codepass);
          int cv = Integer.parseInt(jTextField9.getText());
     
       int cca =  (Integer) mainscreen.apk.getRecord(cv);//ανάγνωση κάρτας        
             
           
            if (jTextField9.getText().isEmpty()) {

            } else  if (cca>0)  {
                 JOptionPane.showMessageDialog(null, "Ο αριθμός CVV υπάρχει: " + cca + "", "Πληκτρολόγησε ένα διαφορετικό CVV", JOptionPane.WARNING_MESSAGE);
           
            } else {
                
           Iterator itr = bank.iterator();
      
            Bank b = (Bank) itr.next();
                   
               b.equals(jComboBox1.getSelectedIndex()); //πάρε την τράπεζα
                
            
                String car = (jTextField8.getText());//μετατροπή
                int cvv = Integer.parseInt(jTextField9.getText());//μετατροπή
                //καταχώρηση στοιχείων σε εξωτερική Βαση Δεδομενων
               System.out.println(b.getBankId() + " " + car + " " + jTextField7.getText() + " " + cvv);

                mainscreen.apk.newRecord(b.getBankId(), jTextField8.getText(), jTextField7.getText(), Integer.parseInt(jTextField9.getText()));
         
           
                
                
            }
            dispose();

        } else {
            mainscreen.db.DeleteCustomer(customer);
            dispose();

        }
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jTextField8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField8ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField8ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
     
    }//GEN-LAST:event_jComboBox1ActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private javax.swing.JTextField jTextField5;
    private javax.swing.JTextField jTextField7;
    private javax.swing.JTextField jTextField8;
    private javax.swing.JTextField jTextField9;
    // End of variables declaration//GEN-END:variables
}
